$(function () {
  $.post('/admin_getAllUser', (res) => {
    // console.log(res);
    if (res.code == 1) {
      $('tbody').html(template('tp', res.data));
    }
  });

  function checkData(eleId, reg, tip) {
    let val = $("#" + eleId).val().trim();
    let result = reg.test(val);
    if (!result) {
      $(".alert").removeClass('hidden').children('span.danger-info').text(tip);
    }
    return result;
  }

  // add-button
  $("#btn-add").on('click', (params) => {
    // check
    if (!checkData('email', /^\w+[@]\w+[.]\w+$/, '邮箱格式不正确')) return;
    if (!checkData('slug', /^.+$/, '别名不能为空')) return;
    if (!checkData('nickname', /^[\u4E00-\u9FA5A-Za-z0-9]+$/, '昵称不正确')) return;
    if (!checkData('password', /^\w{6,14}$/, '密码格式不正确')) return;

    let data = $("#form").serialize();
    $.post('/admin_adduser', data, (res) => {
      if (res.code == 1) {
        alert('添加成功');
        location.reload();
      } else {
        alert('添加失败');
      }
    });
  });

  // eidt button
  $("tbody").on('click', '.edit', function () {
    let id = $(this).parent().parent().attr('data-userid');
    $.post('/admin_getUserInfoById', {
      id: id
    }, (res) => {
      if (res.code == 1) {
        let form = $("#form");
        if (form.children().eq(0).attr('type') != "hidden") {
          form.prepend('<input type="hidden" name="id" value="' + res.data.id + '">');
        } else {
          form.children().eq(0).val(res.data.id);
        }
        $("#email").val(res.data.email);
        $("#slug").val(res.data.slug);
        $("#nickname").val(res.data.nickname);
        $("#password").val(res.data.password);

        $('.edit-button').removeClass('hidden').prev().addClass('hidden');
      }
    });
  });

  // save button
  $("#btn-save").on('click', () => {
    if (!checkData('email', /^\w+[@]\w+[.]\w+$/, '邮箱格式不正确')) return;
    if (!checkData('slug', /^.+$/, '别名不能为空')) return;
    if (!checkData('nickname', /^[\u4E00-\u9FA5A-Za-z0-9]+$/, '昵称不正确')) return;
    if (!checkData('password', /^\w{6,14}$/, '密码格式不正确')) return;

    let data = $("#form").serialize();

    $.post('/admin_updateUserInfo', data, (res) => {
      if (res.code == 1) {
        alert(res.message);
        location.reload();
      }
    });
  });

  // cancel button
  $("tbody").on('click', '.del', function () {
    let id = $(this).parent().parent().attr('data-userid');
    if (confirm('确定要删除吗？')) {
      $.post('/admin_delUserById', {
        id
      }, (res) => {
        if (res.code == 1) {
          alert('删除成功');
          location.reload();
        }
      });
    }
  });

  // check multiple
  $("thead input[type=checkbox]").on('click', function () {
    let status = $(this).prop('checked');
    $('tbody input[type=checkbox]').prop('checked', status);
  });

  $("tbody").on('click', 'input[type=checkbox]', () => {
    let checked = $('tbody input:checked');
    let checkAll = checked.size() == $('tbody input').size();
    $("thead input[type=checkbox]").attr('checked', checkAll);
    if (checked.size() > 1) {
      $("#del-multiple").show();
    } else {
      $("#del-multiple").hide();
    }
  });

  // multiple delete
  $("#del-multiple").on('click', () => {
    if (!confirm('确定要删除这些数据吗？')) return;
    let ids = [];
    let checked = $("tbody input:checked");
    checked.each((i, e) => {
      let id = $(e).parent().parent().attr('data-userid');
      ids.push(id);
    });
    // console.log(ids);
    $.get('/admin_delMultiple', {
      ids: ids
    }, (res) => {
      if (res.code == 1) {
        alert('删除成功');
        location.reload();
      }
    });
  });

});